const Router = require('koa-router')
let route = new Router()

const mongoose = require('mongoose')
const fs = require('fs')
const { data } = require('autoprefixer')

route.get('/insertAllGoodsInfo',async(ctx)=>{
  fs.readFile('./data_json/newGoods.json','utf8',(err,data)=>{
    data = JSON.parse(data)
    let saveCount = 0
    const Goods = mongoose.model('Goods')
    data.map((val,index)=>{
      console.log(val)
      let newGoods = new Goods(val)
      newGoods.save().then(()=>{
        saveCount++
        console.log('成功'+saveCount)
      }).catch(err=>{
        console.log(err)
      })
    })
  })
  ctx.body = '开始导入数据'
})

route.get('/insertAllCategory',async(ctx)=>{
  fs.readFile('./data_json/category.json','utf8',(err,data)=>{
    data = JSON.parse(data)
    let saveCount = 0
    const Category = mongoose.model('Category')
    data.RECORDS.map((val,index)=>{
      console.log(val)
      let newCategory = new Category(val)
      newCategory.save().then(()=>{
        saveCount++
        console.log('成功'+saveCount)
      }).catch(err=>{
        console.log(err)
      })
    })
  })
  // 使用route.get中必须加上ctx.body
  // 否则报404
  ctx.body = '数据开始导入'
})

route.get('/insertAllCategorySub',async(ctx)=>{
  fs.readFile('./data_json/category_sub.json','utf8',(err,data)=>{
    data = JSON.parse(data)
    const CategorySub = mongoose.model('CategorySub')
    let saveCount = 0
    data.RECORDS.map((val,index)=>{
      console.log(val)
      let newCategorySub = new CategorySub(val)
      newCategorySub.save().then(()=>{
        saveCount++
        console.log('成功'+saveCount)
      }).catch(err=>{
        console.log(err)
      })
    })
  })
  ctx.body = '数据开始导入'
})



// 获取商品详情信息的接口
route.post('/getDateilGoodsInfo',async(ctx)=>{
  let goodsId = ctx.request.body.goodsId
  const Goods = mongoose.model('Goods')
  await Goods.findOne({ID:goodsId}).exec()
  .then(async(result)=>{
    ctx.body = {
      code:200,
      message:result
    }
  }).catch(err=>{
    console.log(err)
    ctx.body = {code:500,message:err}
  })
})


// 读取大类数据
route.get('/getCategoryList',async(ctx)=>{
  try{
    const Category = mongoose.model('Category')
    let result = await Category.find().exec()
    ctx.body={code:200,message:result}

  }catch(error){
    ctx.body={code:500,message:error}
  }
})

// 读取小类的数据的接口
route.post('/getCategorySubList',async(ctx)=>{
  try{
    // let categoryId = 1
    let categoryId = ctx.request.body.categoryId
    // console.log(ctx.request.body.categoryId)
    const CategorySub = mongoose.model('CategorySub')
    let result = await CategorySub.find({MALL_CATEGORY_ID:categoryId}).exec()
    ctx.body={code:200,message:result}

  }catch(error){
    ctx.body={code:500,message:error}
  }
})

// 根据类别获取商品列表
route.post('/getGoodsListByCategorySubID',async(ctx)=>{
  try{
    let categorySubId = ctx.request.body.categorySubId //子类别ID
    let page = ctx.request.body.page //当前页数
    let num = 10 //每页显示数量
    let start = (page-1)*num //开始的位置
    // let categorySubId = '2c9f6c946016ea9b016016f79c8e0000'
    const Goods = mongoose.model('Goods')
    let result = await Goods.find({SUB_ID:categorySubId})
    //skip跳过 limit 每页显示数量
    .skip(start).limit(num).exec()
    ctx.body={code:200,message:result}

  }catch(error){
    ctx.body={code:500,message:error}
  }
})

module.exports = route